class AddAllSections < ActiveRecord::Migration
  def self.up
    days = []
    start_times = []
    end_times = []
    buildings = ['Skiles', 'Howey Physics', 'Whitaker', 'Ford', 'Paper Science', 'Couch', 'CoC', 'Klaus']
    
    instructor_id_max = Instructor.find(:all).last.id
    current_semester_id = Semester.current_and_future.first.id
    
    #generate hour-long times
    (8..18).each { |e| start_times << Time.parse(e.to_s + ':05').to_s(:db); end_times << Time.parse(e.to_s + ':55').to_s(:db); days << 'MWF' }
    mwf_max = start_times.length
    #generate hour-and-a-half long times
    (8..18).each { |e| start_times << Time.parse(e.to_s + ':05').to_s(:db); end_times << Time.parse((e+1).to_s + ':35').to_s(:db); days << 'TR' }

    Course.find(:all).each do |course|
      ['A', 'B', 'C', 'D'].each do |title|
        time_index = rand(start_times.length)
        Section.create( :title => title, 
                        :start_time => start_times[time_index], 
                        :end_time => end_times[time_index], 
                        :days => days[time_index],
                        :building => buildings[rand buildings.length],
                        :room => rand(999).to_s,
                        :instructor_id => rand(instructor_id_max)+1,
                        :course_id => course.id,
                        :semester_id => current_semester_id)
      end
    end
    delete_course_ids = ['215', '217', '198', '185', '175', '167', '54', '45', '42', '39', '35', '33', '30', '29', '27', '25', '212', '216', '125', '2', '218']
    Section.delete_all "course_id = " + delete_course_ids.join(' or course_id = ')
    
    #some for the current semester
    Section.create( :title => 'K',
                    :start_time => Time.parse("8:05"),
                    :end_time => Time.parse("8:55"),
                    :days => 'MWF',
                    :building => 'skiles',
                    :room => '123',
                    :instructor_id => 5,
                    :course_id => 212,
                    :semester_id => current_semester_id,
                    :registered => true)
    Section.create( :title => 'F',
                    :start_time => Time.parse("9:05"),
                    :end_time => Time.parse("9:55"),
                    :days => 'MWF',
                    :building => 'skiles',
                    :room => '123',
                    :instructor_id => 5,
                    :course_id => 216,
                    :semester_id => current_semester_id,
                    :registered => true)
    Section.create( :title => 'M',
                    :start_time => Time.parse("10:05"),
                    :end_time => Time.parse("11:35"),
                    :days => 'TR',
                    :building => 'skiles',
                    :room => '123',
                    :instructor_id => 5,
                    :course_id => 125,
                    :semester_id => current_semester_id,
                    :registered => true)
    Section.create( :title => 'K',
                    :start_time => Time.parse("10:05"),
                    :end_time => Time.parse("10:55"),
                    :days => 'MWF',
                    :building => 'skiles',
                    :room => '123',
                    :instructor_id => 5,
                    :course_id => 2,
                    :semester_id => current_semester_id,
                    :registered => true)
    Section.create( :title => 'K',
                    :start_time => Time.parse("4:05"),
                    :end_time => Time.parse("4:55"),
                    :days => 'MWF',
                    :building => 'skiles',
                    :room => '123',
                    :instructor_id => 5,
                    :course_id => 218,
                    :semester_id => current_semester_id,
                    :registered => true)
    Section.create( :title => 'J',
                    :start_time => Time.parse("5:05"),
                    :end_time => Time.parse("5:55"),
                    :days => 'MWF',
                    :building => 'skiles',
                    :room => '123',
                    :instructor_id => 6,
                    :course_id => 218,
                    :semester_id => current_semester_id,
                    :registered => true)
    Section.create( :title => 'L',
                    :start_time => Time.parse("6:05"),
                    :end_time => Time.parse("7:55"),
                    :days => 'MWF',
                    :building => 'skiles',
                    :room => '123',
                    :instructor_id => 7,
                    :course_id => 218,
                    :semester_id => current_semester_id,
                    :registered => true)
    
    #make some full
    max_sections = Section.find(:all).last.id
    ids = []
    r = 0
    100.times do
      until Section.find(:all).collect{|e| e.id}.include?(r)
        r = rand(max_sections)+1
      end
      sec = Section.find(r)
      sec.full = true
      sec.save
    end
    
  end

  def self.down
    Section.delete_all
  end
end
